// pages/allorder/allorder.js
var QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: {
      selectAll: app.data.ull + "wxSelectAllOrder",
      selectOne: "../orderinfo/orderinfo",
      takeOrder: app.data.ull + "wxTakeOrder",
    },
    allorder: "",
    index: 0,
    Showview: "none",
    fettlerid: "",
    location: {},
    markers: [],
    callout: {
      content: "",
      bgColor: "yellow",
      display: 'BYCLICK',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var markers = that.data.markers;
    qqmapsdk = new QQMapWX({
      key: '56RBZ-4Q2E6-NDVSZ-MHHJB-JRUKO-YNFCO'
    });
    wx.getLocation({
      success: function (res) {
        var marker = {
          id: 0,
          latitude: res.latitude,
          longitude: res.longitude,
          callout: {
            content: "当前",
            color: "black",
            bgColor: "green",
            display: 'ALWAYS',
          },

        };
        markers.push(marker);
        that.setData({
          markers: markers,
          location: res,
        })
      }
    })


  },
  /**
   *确认下单
   */
  takeOrder: function (e) {
    var that = this;
    var orderid = that.data.allorder[e.target.id].number;
    wx.showModal({
      title: '是否接单',
      content: '',
      confirmText: '确认接单',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.url.takeOrder,
            data: {
              orderId: orderid,
              fettlerId: app.globalData.id,
            },
            header: {},
            method: "GET",
            dataType: "json",
            success: function (res) {
              wx.navigateTo({
                url: that.data.url.selectOne + '?orderId=' + orderid,
              })
            },
            fail: function (res) { },
            complete: function (res) {
            },
          })
        }
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    wx.showNavigationBarLoading();
    var that = this;
    wx: wx.request({
      url: that.data.url.selectAll,
      data: {
      },
      header: {},
      method: "GET",
      dataType: "json",
      success: function (res) {
        var showview = "";
        var allorder = res.data;
        var markers = that.data.markers;
        if (res.data.length == 0) {
          showview = "block";
        } else {
          showview = "none";
        }
        // for(var j in res.data){
        // qqmapsdk.geocoder({
        //   address: res.data[j].addressname,
        //   success: function (res) {
        //     console.log("所有订单",allorder);
        //     var marker={
        //       id:j,
        //       latitude: res.result.location.lat,
        //       longitude: res.result.location.lng,
        //       callout:{
        //         content: allorder[j].type + allorder[j].content,
        //         bgColor: "green",
        //         display: 'BYCLICK',
        //       },
        //     };
        //    markers.push(marker);
        //   },
        //   fail: function (res) {
        //   },
        //   complete: function (res) {
        //   }
        //   });
        // }
        // for(var j in allorder){
        //   for(var i in that.data.phone ){
        //     if(allorder[j].userId==phone[i].id){
        //       allorder[j].userId = phone[i].phone;
        //       break;
        //     } 
        //   }
        // }
        that.setData({
          allorder: allorder,
          showview: showview,
        })
        console.log("order", allorder);
      },
      fail: function (res) { },
      complete: function (res) { },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 用户在地图上查看维修地址
   */
  showAddress: function (e) {
    var index = e.target.id;
    var that = this;
    var markers = that.data.markers;
    var order = that.data.allorder[index];
    console.log("所有", order);
    qqmapsdk.geocoder({
      address: order.addressname,
      success: function (res) {
        console.log("所有订单", order.addressname);
        var marker = {
          id: index,
          latitude: res.result.location.lat,
          longitude: res.result.location.lng,
          callout: {
            content: order.type + order.content,
            bgColor: "blue",
            display: 'BYCLICK',
          },
        };
        markers.push(marker);
      },
      fail: function (res) {
      },
      complete: function (res) {
      }
    });
    that.setData({
      markers: markers,
    })

  },
  gofirst:function(){
    wx.switchTab({
      url: '../index/index',
    })
  }
})